/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.main;
import view.actor.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Font;
import model.game.*;
import java.util.ArrayList;
import model.map.cell.TeleportCell;
import model.utility.ImageUtility;

/**
 *
 * @author Satrio Dewantono
 */
public class DrawBoard extends JPanel {
  private BackGround bg;
  private PlayerActor ps;
  private GameEngine game_;
  private InformationLayer il_;
  private SpeechBubbleManager sbm_;
  private Actors actors;
  private Image nightoverlay;

  public DrawBoard(BackGround bckgr,PlayerActor plyr,GameEngine eng,SpeechBubbleManager sbm,Actors act){
    bg = bckgr;
    ps = plyr;
    game_ = eng;
    il_ = new InformationLayer(game_);
    sbm_ = sbm;
    actors = act;
    nightoverlay = ImageUtility.loadImage("resources/pictures/nightoverlay.png");
  }

  public void drawBackGround(Graphics2D g){
    int i ;
    int j;
    ArrayList<ArrayList<Image>> drawlist = bg.getDrawList();
    Graphics2D g2d = g;
    for (i = 0;i < drawlist.size(); i++){
      for (j = 0;j < drawlist.get(i).size();j++){
	g2d.drawImage(drawlist.get(i).get(j), i*25+bg.getX(), j*25+bg.getY(), this);
      }
    }
  }

   public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D)g;
    drawBackGround(g2d);
    if (il_.getCurrentMapName() == "Field"){
      for (int i = 0; i < actors.getStaticActorList().size();i++){
	if(actors.getStaticActorList().get(i).getAbsY()<=bg.getabsY()){
	  g2d.drawImage(actors.getStaticActorList().get(i).getImage(),actors.getStaticActorList().get(i).getX(),actors.getStaticActorList().get(i).getY()-actors.getStaticActorList().get(i).getImage().getHeight(this)+25,this);
	}
      }
      for (int i = 0; i < actors.getMovingActorList().size();i++){
	if(actors.getMovingActorList().get(i).getAbsY()<=bg.getabsY()){
	  g2d.drawImage(actors.getMovingActorList().get(i).getImage(),actors.getMovingActorList().get(i).getX(),actors.getMovingActorList().get(i).getY()-actors.getStaticActorList().get(i).getImage().getHeight(this)+25,this);
	}
      }
    }
    g2d.drawImage(ps.getImage(), ps.getX(), ps.getY(), this);
    if (il_.getCurrentMapName() == "Field"){
      for (int i = 0; i < actors.getStaticActorList().size();i++){
	if(actors.getStaticActorList().get(i).getAbsY()>bg.getabsY()){
	  g2d.drawImage(actors.getStaticActorList().get(i).getImage(),actors.getStaticActorList().get(i).getX(),actors.getStaticActorList().get(i).getY()-actors.getStaticActorList().get(i).getImage().getHeight(this)+25,this);
	}
      }
      for (int i = 0; i < actors.getMovingActorList().size();i++){
	if(actors.getMovingActorList().get(i).getAbsY()>bg.getabsY()){
	  g2d.drawImage(actors.getMovingActorList().get(i).getImage(),actors.getMovingActorList().get(i).getX(),actors.getMovingActorList().get(i).getY()-actors.getStaticActorList().get(i).getImage().getHeight(this)+25,this);
	}
      }
    }
    g2d.setFont(new Font("serif", Font.PLAIN, 15));
    //g2d.drawString("player cell :"+game_.getCreatures().getPlayer().getCell().getX()+","+game_.getCreatures().getPlayer().getCell().getY(),15 ,15);
    //g2d.drawString("player cell :"+bg.getabsX()+","+bg.getabsY(),15 ,28);
    /*if (game_.getCreatures().getPlayer().getCell().isTeleport()){
      TeleportCell tc = (TeleportCell)game_.getCreatures().getPlayer().getCell();
      if (tc.getTeleportDestination() == null){
	g2d.drawString("null!",15,41);
      }else{
	g2d.drawString("teleports to :"+tc.getTeleportDestination(),15,41);
      }
    }*/
    g2d.drawString("money :"+il_.getMoney(), 15,15);
    g2d.drawString(il_.getError()+il_.getMessage(), 15,41);
    if (!il_.isNight()){
      g2d.drawString("day: "+il_.getCurrentDay()+" time :"+il_.getCurrentTime(), 15,28);
    }else{
      g2d.drawString("time :"+il_.getNightTime(), 15,28);
    }
    g2d.drawImage(il_.getCurrentInventoryImage(),300 ,0,this);
    //g2d.drawImage(il_.getCurrentToolImage(),15 ,41,this);
    if (sbm_.doIHaveAmessage()){
      g2d.drawImage(sbm_.getImage(),sbm_.getX(),sbm_.getY(),this);
      if (sbm_.isShowingToolBubble()){
	g2d.drawImage(il_.getCurrentToolImage(),sbm_.getX()+2,sbm_.getY()+4,this);
      }
      if (sbm_.getImage2()!=null){
	g2d.drawImage(sbm_.getImage2(),sbm_.getX2(),sbm_.getY2(),this);
      }
    }
    if(il_.isNight()){
      g2d.drawImage(nightoverlay,0,0,this);
    }

    Toolkit.getDefaultToolkit().sync();
    g.dispose();
  }
}
